/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminOperations;

import CRUD.ForumHome;
import CRUD.MemberHome;
import CRUD.ModeratorpermessionsHome;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
import xmlMapping.Member;
import xmlMapping.Moderatorpermessions;
import java.util.List;
import java.util.Set;
import xmlMapping.Forum;
/**
 *
 * @author ABDELMOHSEN
 */
public class AdminManageModerator {

    public void demoteModerator(int moderatId){
        MemberHome mh=new MemberHome();
        ForumHome fHome=new ForumHome();
        Member memb=mh.findById(moderatId);
        memb.setType("mem");
        Iterator<Forum> memforums=memb.getForums().iterator();
        while(memforums.hasNext()){
            Forum forum=fHome.findById(memforums.next().getForumId());
            Set<Member> mods=forum.getMembers();
            mods.remove(memb);
            forum.setMembers(mods);
        }
        mh.persist(memb);
    }




    public void editModeratorPermission(Moderatorpermessions modperm){
        ModeratorpermessionsHome mph=new ModeratorpermessionsHome();
        mph.merge(modperm);
    }


    public Moderatorpermessions getModeratorpermessions(int moderatId){
        ModeratorpermessionsHome mph= new ModeratorpermessionsHome();
        return mph.findById(moderatId);
    }



    public Vector<Member> getModerators(){
        MemberHome mh=new MemberHome();
        List<Member> lst=mh.findMemberByType("mod");
        Vector<Member> moderators=new Vector<Member>(lst);
        return moderators;
    }


   /*public static void main (String[] args){
       AdminManageModerator admin=new AdminManageModerator();
       Vector<Member> vec=admin.getModerators();
       for(int i=0;i<vec.size();i++){
           System.out.println("moderator" + vec.elementAt(i).getFname());
       }


       //  to test permission
       System.out.println("Permissoin" + admin.getModeratorpermessions(4).getMemberId());



       //test edit permission
       Moderatorpermessions modper=admin.getModeratorpermessions(4);
       modper.setEditpost(true);
       System.out.println(modper.getMemberId() + modper.getMember().getMemberId());
       admin.editModeratorPermission(modper);

       admin.demoteModerator(4);
   }*/

}

